﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class EditContractDate : BasePage
    {
        Contracts contract;
        int contractID;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            contractID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;

            if (!IsPostBack)
                LoadInfo();

            addService.DisableAmountEdit();
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            Save();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            try
            {
                contract = new Contracts();
                contract.LoadByPrimaryKey(contractID);

                contract.LastModifiedByUser = SessionUser.ID;
                contract.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                contract.PlanDescription = addService.PlanDescription;
                contract.PlanDuration = addService.PlanDuration;
                //contract.Rebate = addService.Rebate;
                //contract.MPP = addService.MPP;
                //contract.Commission = addService.Commission;
                contract.NetworkID = addService.NetworkID;
                contract.MobileNumber = addService.MobileNumber;
                //contract.Network = addService.Network;
                if (addService.StartDate != null)
                    contract.ContractStartDate = (DateTime)addService.StartDate;
                if (addService.EndDate != null)
                    contract.ContractEndDate = (DateTime)addService.EndDate;
                if (addService.CreatedDate != null)
                    contract.ContractCreatedDate = (DateTime)addService.CreatedDate;

                contract.Save();

                ((Popup)Master).ShowMessage("Saved");

                View_Contracts contracts = (View_Contracts)Session[Request.QueryString[KEY]];
                if (contracts != null)
                {
                    string oldViewFilter = contracts.Filter;
                    contracts.Filter = View_Contracts.ColumnNames.ID + "=" + contractID.ToString();

                    if (contracts.RowCount > 0)
                    {
                        contracts.PlanDescription = addService.PlanDescription;
                        contracts.PlanDuration = addService.PlanDuration;
                        //contract.Rebate = addService.Rebate;
                        //contract.MPP = addService.MPP;
                        //contract.Commission = addService.Commission;
                        contracts.NetworkID = addService.NetworkID;
                        contracts.MobileNumber = addService.MobileNumber;
                        contracts.NetworkName = addService.Network;
                        if (addService.StartDate != null)
                            contracts.ContractStartDate = (DateTime)addService.StartDate;
                        if (addService.EndDate != null)
                            contracts.ContractEndDate = (DateTime)addService.EndDate;
                        if (addService.CreatedDate != null)
                            contracts.ContractCreatedDate = (DateTime)addService.CreatedDate;
                    }

                    contracts.Filter = oldViewFilter;
                }

                return true;
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("addService.aspx.cs", "Save", ex.StackTrace);
                return false;
            }
        }

        private void LoadInfo()
        {
            try
            {
                contract = new Contracts();
                contract.LoadByPrimaryKey(contractID);

                if (!contract.IsColumnNull(Contracts.ColumnNames.MPP))
                    addService.MPP = contract.MPP;
                if (!contract.IsColumnNull(Contracts.ColumnNames.Rebate))
                    addService.Rebate = contract.Rebate;
                if (!contract.IsColumnNull(Contracts.ColumnNames.NetworkID))
                    addService.NetworkID = contract.NetworkID;
                if (!contract.IsColumnNull(Contracts.ColumnNames.PlanDescription))
                    addService.PlanDescription = contract.PlanDescription;
                if (!contract.IsColumnNull(Contracts.ColumnNames.PlanDuration))
                    addService.PlanDuration = contract.PlanDuration;
                if (!contract.IsColumnNull(Contracts.ColumnNames.MobileNumber))
                    addService.MobileNumber = contract.MobileNumber;
                if (!contract.IsColumnNull(Contracts.ColumnNames.Commission))
                    addService.Commission = contract.Commission;
                if (!contract.IsColumnNull(Contracts.ColumnNames.ContractStartDate))
                    addService.StartDate = contract.ContractStartDate;
                else
                    addService.StartDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                if (!contract.IsColumnNull(Contracts.ColumnNames.ContractEndDate))
                    addService.EndDate = contract.ContractEndDate;
                if (!contract.IsColumnNull(Contracts.ColumnNames.ContractCreatedDate))
                    addService.CreatedDate = contract.ContractCreatedDate;
                else
                    addService.CreatedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("addService.aspx.cs", "LoadInfo", ex.StackTrace);
            }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (Save())
                CloseDlg(1);
        }
    }
}